﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JzSport.Model
{
    public class PromotionalActivityService
    {
        public ServiceResult<PagedList<PromotionalActivity>> GetPagedList(string city, int start, int limit)
        {
            using (JzSportDbContext dbContext = new JzSportDbContext())
            {
                EntityRepository<PromotionalActivity> reps = new EntityRepository<PromotionalActivity>(dbContext);
                PagedList<PromotionalActivity> pagedList = new PagedList<PromotionalActivity>();
                pagedList.Total = dbContext.Database.SqlQuery<int>("exec [SP_Sport_GetPromotionalActivity] @returnlist=0,@city={0}", city).SingleOrDefault();
                pagedList.Items = dbContext.Database.SqlQuery<PromotionalActivity>("exec [SP_Sport_GetPromotionalActivity] @returnlist=1,@city={0},@start={1},@limit={2}",
                    city, start, limit).ToList();
                pagedList.Start = start;
                pagedList.Limit = limit;
                return new ServiceResult<PagedList<PromotionalActivity>>(ErrorCode.NoError, pagedList);
            }
        }

        [Obsolete("使用另外一个重载")]
        public ServiceResult<bool> Create(PromotionalActivity activity)
        {
            using (JzSportDbContext dbContext = new JzSportDbContext())
            {
                EntityRepository<PromotionalActivity> reps = new EntityRepository<PromotionalActivity>(dbContext);

                reps.Create(activity);

                if (dbContext.SaveChanges() > 0)
                {
                    return new ServiceResult<bool>(ErrorCode.NoError, true);
                }
                return new ServiceResult<bool>(ErrorCode.DB_Error, false);
            }
        }

        /// <summary>
        /// 
        /// 创建推广活动
        /// </summary>
        /// <param name="title"></param>
        /// <param name="categoryCode"></param>
        /// <param name="address"></param>
        /// <param name="city"></param>
        /// <param name="longitude"></param>
        /// <param name="latitude"></param>
        /// <param name="beginTime"></param>
        /// <param name="endTime"></param>
        /// <param name="maxPersonCount"></param>
        /// <param name="costPrePerson"></param>
        /// <param name="description"></param>
        /// <param name="previewImageUrl"></param>
        /// <param name="sponsor"></param>
        /// <param name="administrators"></param>
        /// <returns></returns>
        public ServiceResult<bool> Create(string title,
            string categoryCode,
            string address,
            string city,
            double longitude,
            double latitude,
            DateTime beginTime,
            DateTime endTime,
            int maxPersonCount,
            decimal costPrePerson,
            string description,
            string previewImageUrl,
            string sponsor,
            int[] administrators)
        {
            ServiceResult<Activity> srActivity = new ActivityService().Create(BasicData.ActivityType.System,
                BasicData.SYSAccountId,
                title, 
                categoryCode,
                address,
                city,
                longitude,
                latitude,
                beginTime,
                endTime,
                maxPersonCount,
                costPrePerson,
                description,
                null,
                null);
            if (srActivity.ErrorCode == ErrorCode.NoError)
            {
                using (JzSportDbContext dbContext = new JzSportDbContext())
                {
                    ActivityMemberRepository amReps = new ActivityMemberRepository(dbContext);
                    EntityRepository<PromotionalActivity> reps = new EntityRepository<PromotionalActivity>(dbContext);
                    PromotionalActivity promotionalActivity = new PromotionalActivity();
                    promotionalActivity.Id = Guid.NewGuid();
                    promotionalActivity.Title = srActivity.Data.Title;
                    promotionalActivity.ActivityId = srActivity.Data.Id;
                    promotionalActivity.BeginTime = srActivity.Data.BeginTime;
                    promotionalActivity.City = srActivity.Data.City;
                    promotionalActivity.CreatedTime = srActivity.Data.CreatedTime;
                    promotionalActivity.EndTime = srActivity.Data.EndTime;
                    promotionalActivity.IsDeleted = false;
                    promotionalActivity.LinkUrl = string.Format("./Promotional/Detail.aspx?id={0}&sys:token=@sys_token", promotionalActivity.Id);
                    promotionalActivity.PreviewImageUrl = previewImageUrl;
                    promotionalActivity.Sponsor = sponsor;
                    reps.Create(promotionalActivity);

                    // 插入管理员
                    if (administrators != null && administrators.Length > 0)
                    {
                        foreach(var administrator in administrators)
                        {
                            ActivityMember member = new ActivityMember();
                            member.ActivityId = srActivity.Data.Id;
                            member.Id = Guid.NewGuid();
                            member.JoinTime = DateTime.Now;
                            member.UserId = administrator;
                            member.Type = BasicData.ActivityMemberType.Administrator;
                            amReps.Create(member);
                        }
                    }

                    if (dbContext.SaveChanges() > 0)
                    {
                        return new ServiceResult<bool>(ErrorCode.NoError, true);
                    }
                    return new ServiceResult<bool>(ErrorCode.DB_Error, false);
                }
            }
            return new ServiceResult<bool>(ErrorCode.DB_Error, false);
        }

        /// <summary>
        /// 
        /// 创建图片连接信息
        /// </summary>
        /// <param name="title"></param>
        /// <param name="city"></param>
        /// <param name="sponsor"></param>
        /// <param name="beginTime"></param>
        /// <param name="endTime"></param>
        /// <param name="previewImageUrl"></param>
        /// <param name="linkUrl"></param>
        /// <returns></returns>
        public ServiceResult<bool> Create(string title, string city, string sponsor,
            DateTime beginTime,
            DateTime endTime,
            string previewImageUrl, string linkUrl)
        {
            using (JzSportDbContext dbContext = new JzSportDbContext())
            {
                EntityRepository<PromotionalActivity> reps = new EntityRepository<PromotionalActivity>(dbContext);
                PromotionalActivity promotionalActivity = new PromotionalActivity();
                promotionalActivity.Id = Guid.NewGuid();
                promotionalActivity.Title = title;
                promotionalActivity.ActivityId = Guid.Empty;
                promotionalActivity.BeginTime = beginTime;
                promotionalActivity.City = city;
                promotionalActivity.CreatedTime = DateTime.Now;
                promotionalActivity.EndTime = endTime;
                promotionalActivity.IsDeleted = false;
                promotionalActivity.LinkUrl = linkUrl;
                promotionalActivity.PreviewImageUrl = previewImageUrl;
                promotionalActivity.Sponsor = sponsor;
                reps.Create(promotionalActivity);


                if (dbContext.SaveChanges() > 0)
                {
                    return new ServiceResult<bool>(ErrorCode.NoError, true);
                }
                return new ServiceResult<bool>(ErrorCode.DB_Error, false);
            }
        }

        public ServiceResult<PromotionalActivity> Get(Guid id)
        {
            using (JzSportDbContext dbContext = new JzSportDbContext())
            {
                EntityRepository<PromotionalActivity> reps = new EntityRepository<PromotionalActivity>(dbContext);

                try
                {
                    PromotionalActivity activity = reps.Get(p => p.Id == id);
                    return new ServiceResult<PromotionalActivity>(ErrorCode.NoError, activity);
                }
                catch(Exception ex)
                {
                    return new ServiceResult<PromotionalActivity>(ErrorCode.DB_Error, null);
                }
            }
        }
    }
}
